import config from "./config.js"
let http = (option = {},num=1) => {
	//console.log('开发环境')
	// if (process.env.NODE_ENV === "development") {
	let leng = uni.getStorageSync('languageDomain');
	// 	if (option.url !== "language/domain") {
	// 		if (leng === "https://m-admin-zh.mhbox.top/") {
	// 			url.baseUrl = "zh_cn/";
	// 		} else if (leng === "https://m-admin-en.mhbox.top/") {
	// 			url.baseUrl = "en/";
	// 		} else if (leng === "https://m-admin-br.mhbox.top/") {
	// 			url.baseUrl = "br/";
	// 		} else if (leng === "https://m-admin.mhbox.top/") {
	// 			url.baseUrl = "tl/";
	// 		}
	// 	}
	// }
	return new Promise((resolve, reject) => {
		let baseUrlItem = "";
		let data = {};
		if (option.url === "language/domain") {
			baseUrlItem = config.languageUrl;
			data = option.data;
		} else {
			baseUrlItem = leng ? leng : config.baseUrl;
			data = option.data == {} ? {
				lang: uni.getStorageSync('mhlanguage') ? uni.getStorageSync('mhlanguage') : uni.getLocale(),
			} : {
				...option.data,
				lang: uni.getStorageSync('mhlanguage') ? uni.getStorageSync('mhlanguage') : uni.getLocale(),
			}
		}
		uni.request({
			url: baseUrlItem + option.url || "",
			data: data,
			method: option.method || "POST",
			header: option.header || {
				"Content-Type": "application/json",
				"Authorization": uni.getStorageSync("user_token") || ''
			},
			timeout:option.timeout || 60000,
			success: res => {
				if (res.header.authorization || res.header.Authorization) {
					uni.setStorageSync('user_token', res.header.authorization || res.header.Authorization);
				}
				resolve(res)
				if(res.data.code == 401){
					if(num>=1){
						uni.removeStorageSync('user_token');
						uni.removeStorageSync('user');
						// uni.showToast({
						// 	title:res.data.msg,
						// 	icon:'none'
						// })
						// uni.redirectTo({
						// 	url:'/pages/my/login'
						// })
						return;
					}
					
					http(option,num + 1).then(res=>{
						resolve(res)
					})
				}
			},
			fail: err => {
				reject(err)
			}
		})
	})
}

export default http